Xceed DataGrid for WPF v7.2 Documentation
Retrieving a parent group

Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.2 > Xceed DataGrid for WPF > Code Snippets > Retrieving a parent group

The following example demonstrates how to retrieve the parent group of the current item using the GetParentGroupFromItem method so that it can be collapsed. The implementation for the CollapseCurrentGroup method is provided below.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
  <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                     Source="{Binding Source={x:Static Application.Current},
                                                      Path=Orders}">
      <xcdg:DataGridCollectionViewSource.GroupDescriptions>
        <xcdg:DataGridGroupDescription PropertyName="ShipCountry"/>
        <xcdg:DataGridGroupDescription PropertyName="ShipCity"/>
      </xcdg:DataGridCollectionViewSource.GroupDescriptions>
    </xcdg:DataGridCollectionViewSource>
  </Grid.Resources>
  <DockPanel>
    <Button Content="Collapse Group"
             Click="CollapseCurrentGroup"
            DockPanel.Dock="Top"/>
    <xcdg:DataGridControl x:Name="OrdersGrid"
                          ItemsSource="{Binding Source={StaticResource cvs_orders}}"
                          DockPanel.Dock="Bottom"/>
  </DockPanel>
</Grid>

The following provides the implementation for the CollapseCurrentGroup method.

VB.NET
Copy Code
Private Sub CollapseCurrentGroup( ByVal sender As Object, ByVal e As RoutedEventArgs )
  Dim context As DataGridContext = Me.OrdersGrid.CurrentContext
  Dim group As CollectionViewGroup = context.GetParentGroupFromItem( context.CurrentItem )
  context.CollapseGroup( group )
End Sub
C#
Copy Code
private void CollapseCurrentGroup( object sender, RoutedEventArgs e )
{
  DataGridContext context = this.OrdersGrid.CurrentContext;
  CollectionViewGroup group = context.GetParentGroupFromItem( context.CurrentItem );
  context.CollapseGroup( group );
}